// components/market-item/market-item.js
const componentBehavior = require('../../js/common/ComponentBehavior')
import {
  alert,
  isEmpty,
  padStart
} from '../../js/newUtil';
import {
  router
} from '../../js/router';
import {
  $ecCountDown
} from '../../components/index.js'

Component({
  behaviors: [componentBehavior],
  /**
   * 组件的属性列表
   */
  properties: {
    Style: { // 展示类型 mode-1：轮播 mode-2：列表
      type: String,
      value: 'mode-1'
    },
    CssName: { // 展示风格，一行多少个 style-1、style-2、style-3
      type: String,
      value: 'style-1',
    },
    item: { // 当前子项的数据
      type: Object,
      value: {}
    },
    Currency: {
      type: String,
      value: '￥'
    },
    priceDigits: {
      value: 2,
      type: Number
    },
    isLogin: {
      type: Boolean,
      value: false
    },
    NoLoginShowPrice: {
      type: Boolean,
      value: false
    },
    /**是否开启oss */
    enableOss: {
      type: Boolean,
      value: false
    },
    imgDomain: {
      type: String,
      value: ''
    },
    type: { // 类型，1拼团 2预售 3限时特价
      type: Number,
      value: 1
    },
    index: { // 当前项下标
      type: Number,
      value: 0
    },
    contentStyle: { // item的背景样式
      type: String,
      value: ''
    },
    componentData: { // 当前首页数据列表项的数据
      type: Object,
      value: {},
    },
  },

  observers: {
    'Style,CssName'() {
      let Style = this.data.Style
      let CssName = this.data.CssName
      let index = this.data.index
      let componentCustomStyle = ''
      if (Style == 'mode-1') {
        // 轮播模式时，需要在内部居中
        componentCustomStyle = 'margin-auto'
      } else {
        // 列表模式
        if (CssName == 'style-1') {
          // 一行一个商品信息
          componentCustomStyle = 'list-style-1'
        } else if (CssName == 'style-2') {
          // 一行两个商品信息
          componentCustomStyle = 'list-style-2'
          if (index % 2 == 0) {
            componentCustomStyle += ' even'
          } else {
            componentCustomStyle += ' odd'
          }
        } else if (CssName == 'style-3') {
          // 一行三个商品信息
          componentCustomStyle = 'list-style-3'
          if (index % 3 == 0) {
            componentCustomStyle += ' left'
          } else if (index % 3 == 1) {
            componentCustomStyle += ' center'
          } else {
            componentCustomStyle += ' right'
          }
        }
      }
      this.setData({
        componentCustomStyle
      })
    },
  },

  lifetimes: {
    attached() {
      this.startInterval()
    },
  },

  pageLifetimes: {
    show() {
      this.startInterval()
    },
    hide() {
      this.clearInterval()
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    componentCustomStyle: '', // 样式
    timeCountDown: {}, // 倒计时
    showInfo: false, // 显示立即抢按钮
  },

  /**
   * 组件的方法列表
   */
  methods: {
    tapItem(e) {
      let item = this.data.item || {}
      let type = this.data.type || 1
      let actualover = item.ActualQuantity >= item.TotalQuantity ? 1 : 0
      let groupStatus = item.GroupStatus
      let id = item.Id
      let productId = item.ProductId
      let IsStart = item.IsStart || 0
      let timeCountDown = this.data.timeCountDown || {}
      let finish = (IsStart && !timeCountDown.c0) ? true : false
      if (type == 3) {
        let ptype = 3
        // 限时特价
        if (finish) {
          alert('活动已结束')
          return
        }
        if (item.PanicStatus == -1 || item.IsStart != 1) {
          ptype = 0
        }
        router.navigate({
          path: "productDetail",
          params: {
            ptype: ptype,
            id: item.ProductId,
            sid: item.FKId,
            discounttype: 2,
            discountid: item.Id
          }
        })
      } else {
        // 拼团、预售
        if (actualover == 1 && groupStatus == 2) {
          alert('商品已被抢光啦！')
          return
        }
        if (groupStatus == 0) {
          alert('活动已经结束啦！')
          return
        }
        router.navigate({
          path: "groupDetail",
          params: {
            ptype: type,
            id: id,
            productId: productId
          }
        })
      }
    },
    startInterval() {
      let that = this
      let item = this.data.item || {}
      let type = this.data.type
      let isStart = false
      if (type == 3) {
        isStart = (item.IsStart == 1) ? true : false
      } else {
        isStart = (item.GroupStatus == 2) ? true : false
      }
      if (isStart) {
        that.timeInterval = setInterval(() => {
          if (isEmpty(item)) {
            return
          }
          let end = new Date(item.EndTime)
          let timer = {
            c0: false
          }
          if (isNaN(end.getFullYear())) {
            timer = {
              c0: false
            }
          } else {
            let diff = end.getTime() - Date.now()
            let dateData = {
              years: 0,
              days: 0,
              hours: 0,
              min: 0,
              sec: 0,
              millisec: 0,
            }
            if (diff < 0) {
              that.clearInterval()
            } else {
              diff = diff / 1000
              if (diff >= 86400) {
                dateData.days = Math.floor(diff / 86400)
                diff -= dateData.days * 86400
              }
              if (diff >= 3600) {
                dateData.hours = Math.floor(diff / 3600)
                diff -= dateData.hours * 3600
              }
              if (diff >= 60) {
                dateData.min = Math.floor(diff / 60)
                diff -= dateData.min * 60
              }
              dateData.sec = Math.round(diff)
              dateData.days = padStart(dateData.days, 2, '0')
              dateData.hours = padStart(dateData.hours, 2, '0')
              dateData.min = padStart(dateData.min, 2, '0')
              dateData.sec = padStart(dateData.sec, 2, '0')
              if (dateData.days == '00' && dateData.hours == '00' && dateData.min == '00' && dateData.sec == '00') {
                timer = {
                  c0: false
                }
                that.clearInterval()
              } else {
                timer = {
                  c0: true,
                  c1: dateData.days,
                  c2: dateData.hours,
                  c3: dateData.min,
                  c4: dateData.sec,
                }
              }
            }
          }
          that.setData({
            timeCountDown: timer
          })
          if (!that.data.showInfo) {
            that.setData({
              showInfo: true
            })
          }
        }, 1000)
      } else {
        if (!isEmpty(item)) {
          if (!that.data.showInfo) {
            that.setData({
              showInfo: true
            })
          }
        }
      }
    },
    clearInterval() {
      clearInterval(this.timeInterval)
    }
  }
})